﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MongoDB.Driver;
using Sustainalytics.Entities.Governance;

namespace Sustainalytics.Governance.Synchronization.Tests
{
    /// <summary>
    /// Summary description for GovernancePdfFilesReaderTests
    /// </summary>
    [TestClass]
    public class GovernancePdfFilesReaderTests
    {
        private const string ConnectionString = "mongodb://localhost:27017/ClientDWTests";

        private IMongoDatabase _database;
        private string _databaseName;

        [TestInitialize]
        public void Initialize()
        {
            var url = MongoUrl.Create(ConnectionString);

            if (string.IsNullOrWhiteSpace(url.DatabaseName))
                throw new ArgumentException("No database specified in the connection string!");

            var client = new MongoClient(url);

            _databaseName = url.DatabaseName;
            _database = client.GetDatabase(url.DatabaseName);


        }

        [TestCleanup]
        public void Cleanup()
        {
            _database.Client.DropDatabase(_databaseName);
        }

        private void InsertPdfChunk()
        {
            var collection = _database.GetCollection<GovernancePdfProfileMetadata>(typeof(GovernancePdfProfileMetadata).Name);

            var pdfProfileMetadata = new GovernancePdfProfileMetadata
            {
                Id = Guid.NewGuid(),
                CompanyId = Guid.Empty,
                FileId = Guid.Empty,
                FileName = string.Empty,
                OperationType = UpdateOperationType.Created,
                LastOperationDateTime = DateTime.UtcNow,
                GovernancePdfType = GovernancePdfType.GovernanceDataProfile
            };

            collection.InsertOne(pdfProfileMetadata);
        }

        [TestMethod]
        public void GetChangedPdfFilesTest()
        {
            InsertPdfChunk();

            var pdfFilesReader = GovernancePdfFilesReader.GetInstance(ConnectionString);

            var changes = pdfFilesReader.GetChangedPdfFiles(DateTime.UtcNow.AddDays(-1));

            Assert.IsTrue(changes.Any(), "The result is different than the expected one!");
        }


    }
}
